package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.bo.shop.ShopContractParamRelationSaveBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.ztgf.shopping.models.service.shop.IShopContractParamRelationService;
import com.ztgf.shopping.models.bo.shop.ShopContractParamRelationBO;
import com.ztgf.shopping.models.dto.shop.ShopContractParamRelationDTO;

/**
 * 店铺合同内容关联表控制器
 *
 * @author YUE
 * @Date 2020-07-21 11:56:38
 */

@RestController
@RequestMapping("/api/shopContractParamRelation")
@Slf4j
@Api(value = "ShopContractParamRelationApiController",tags = "店铺合同内容关联表Api接口")
public class ShopContractParamRelationApiController {

    @DubboReference
    private IShopContractParamRelationService shopContractParamRelationService;

    /**
     *
     * @param shopId 店铺id
     * @param type 0现货 1预售 2赊销
     * @return
     */
    @PostMapping("/getByShopAndType")
    @ApiOperation(value = "根据店铺id和类型查找",notes = "根据店铺id和类型查找")
    public Result getByShopAndType(Long shopId,int type){
        if(null == shopId){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        ShopContractParamRelationDTO shopContractParamRelationDTO = shopContractParamRelationService.getByShopIdAndType(shopId, type);
        return new Result(shopContractParamRelationDTO);
    }

    @ApiOperation(value = "修改店铺合同内容关联表",notes = "修改店铺合同内容关联表")
    @PostMapping("/update")
    public Result update(ShopContractParamRelationSaveBO saveBO){
        ShopContractParamRelationDTO dto = new ShopContractParamRelationDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = shopContractParamRelationService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopContractParamRelationDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopContractParamRelationDTO dto = shopContractParamRelationService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "店铺合同内容关联表列表",notes = "店铺合同内容关联表列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopContractParamRelationDTO>> queryPageList(ShopContractParamRelationBO bo){
        Wrapper<ShopContractParamRelationDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<ShopContractParamRelationDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = shopContractParamRelationService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
